﻿var currentPage = 1;
var lastPage = 1
var pageSize = 3;
var commentPid = 1;     //评论文章ID。
var scriptPath = "/jsPlugDemo/jTemplates/comment1_api.aspx";
var templatePath = "/jsPlugDemo/jTemplates/template/commentList.htm";


$(document).ready(function () {
    // On page load, display the first page of results.
    GetCommentList(commentPid, currentPage, pageSize);

    // Simultaneously, begin loading the total item count.
    GetRSSItemCount();
});

function GetRSSItemCount() {
    $.ajax({
        type: "POST",
        url: scriptPath + "/GetCommnetCount",
        data: "{'id':'" + commentPid + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            lastPage = Math.ceil(msg.d / pageSize);

            UpdatePaging();
        },
        error: function () {
            alert("GetRSSItemCount error status:" + arguments[0].status);
        }
    });
}
function UpdatePaging() {

    $('#totalRecord').html(currentPage + '/' + lastPage);

    if (currentPage != 1) {
        $('#PrevPage').attr('href', '#');
        $('#PrevPage').click(PrevPage);
    }
    if (currentPage != lastPage) {
        $('#NextPage').attr('href', '#');
        $('#NextPage').click(NextPage);
    }
}
function NextPage(evt) {
    evt.preventDefault();
    //DisplayProgressIndication();
    GetCommentList(commentPid, ++currentPage, pageSize);
}
function PrevPage(evt) {
    evt.preventDefault();
    //DisplayProgressIndication();
    GetCommentList(commentPid, --currentPage, pageSize);
}

function ApplyTemplate(msg) {

    $('#comment').setTemplateURL(templatePath, null, { filter_data: false });
    $('#comment').processTemplate(msg);
}

function GetCommentList($ID, $Page, $PageSize) {

    $.ajax({
        url: scriptPath + "/GetCommentList",
        data: "{ 'id': '" + $ID + "', 'page': '" + $Page + "','pagesize':'" + $PageSize + "' }",
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {

            ApplyTemplate(msg);

            UpdatePaging();

        },
        error: function () {
            alert("GetCommentList error status:" + arguments[0].status);
        }
    });
}

function StopButton() {
    document.getElementById(arguments[0]).disabled = true;
    document.getElementById(arguments[0]).value = "提交(" + arguments[1] + ")";
    if (--arguments[1] > 0) {
        window.setTimeout("StopButton('" + arguments[0] + "'," + arguments[1] + ")", 1000);
    }
    if (arguments[1] <= 0) {
        document.getElementById(arguments[0]).value = '提交';
        document.getElementById(arguments[0]).disabled = false;
    }
}
function SendComment() {

    var $CommentParentID = arguments[0];
    var $CommentUser = $('#CommentUser').val();
    var $CommentText = $('#CommentText').val();
    var $CommentValidate = $('#CommentValidate').val();
    if ($.trim($CommentUser) == '') {
        alert('请您填写昵称！');
        $('#CommentUser').focus();
        return false;
    }
    if ($.trim($CommentValidate) == '') {
        alert('请您填写验证码！');
        $('#CommentValidate').focus();
        return false;
    }
    if ($.trim($CommentText) == '') {
        alert('请您填写回复内容！');
        $('#CommentText').focus();
        return false;
    }
    if ($CommentText.length < 5 || $CommentText.length > 200) {
        alert('内容必须在5-200字之间！');
        return false;
    }
    StopButton('CommentSubmit', 10);

    $.ajax({
        url: scriptPath + "?action=ajax_sendcomment&commentparentid=" + $CommentParentID + "&commentuser=" + escape($CommentUser) + "&commenttext=" + escape($CommentText) + "&commentvalidate=" + escape($CommentValidate) + "&time=" + new Date().toString(),
        type: 'GET',
        success: function () {
            if (arguments[0] == 'ERROR') {
                alert('验证码出错！');
            }
            else {
                GetCommentList($CommentParentID, 1, pageSize);
                alert(arguments[0]);
                $("#CommentText").val("");
                //验证成功时，刷新验证码图片
                $("#CommentValidateImages").attr("src", "/VerifyCode.aspx?s=" + Math.random());
            }
            $("#CommentValidate").val("");
        }
    });
    //SetCookie("CommentUser",$CommentUser,3);
}